package com.kabao.javafx.qq;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.BorderPane;
import javafx.scene.robot.Robot;
import javafx.stage.Stage;
import javafx.util.Duration;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @author LeeWyatt
 * QQ 9670453
 *
 * 此类仅用于连续截图使用
 */
public class UtilFrameApp extends Application {
    private ArrayList<WritableImage> imgList = new ArrayList<>(260);

    @Override
    public void start(Stage primaryStage) throws Exception {
        Button button = new Button("截图");
        Robot robot = new Robot();
        Label label = new Label("准备");
        button.setOnAction(event -> {
            label.setText("开始截屏");
            label.setText("完成..");

        });

        //作为一个定时器使用
        Timeline timeline = new Timeline(new KeyFrame(Duration.millis(100), event -> {
            //截图的设置: 屏幕分辨率 1920*1080 设置的屏幕缩放100%
            // TODO 根据自己的情况设置截图的位置与宽高; 如果需要多倍图,那么放大屏幕后再次截屏,命名后缀为xx@2x.png
            imgList.add(robot.getScreenCapture(null, 745, 375, 428, 126));
        }));
        timeline.setCycleCount(10 * 23);
        timeline.setOnFinished(event -> {
            for (int i = 0; i < imgList.size(); i++) {
                BufferedImage img = SwingFXUtils.fromFXImage(imgList.get(i), null);
                try {
                    ImageIO.write(img, "png",
                            //TODO 修改为自己的截图路径
                            new File("C:\\Users\\Administrator\\Desktop\\loginframe\\login" + i + ".png"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            label.setText("完成");
            imgList.clear();

        });
        button.setOnAction(event -> {
            label.setText("开始截屏");
            timeline.play();

        });
        BorderPane root = new BorderPane();

        root.setCenter(button);
        root.setBottom(label);

        primaryStage.setTitle("截图序列帧图");
        primaryStage.setX(1200);
        primaryStage.setY(450);
        primaryStage.setScene(new Scene(root, 280, 160));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
